package com.carlinfo.back.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.carlinfo.common.servlet.BaseServlet;
import com.carlinfo.common.util.ConstantFinalUtil;
import com.carlinfo.common.util.EncryptUtil;
import com.carlinfo.userscenter.users.pojo.AAdmins;
import com.carlinfo.userscenter.users.pojo.AAdminsEnum;
import com.carlinfo.userscenter.users.service.IUsersService;
import com.carlinfo.userscenter.users.service.impl.UsersServiceImpl;

public class NoLoginBackServlet extends BaseServlet
{
	IUsersService userservice = new UsersServiceImpl();
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		String method = request.getParameter("method");
		if ("login".equalsIgnoreCase(method))
		{
			login(request, response);
		}else if("loginSubmit".equalsIgnoreCase(method))
		{
			loginSubmit(request,response);
		}
	}

	private void loginSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		HttpSession session = request.getSession();
		/* 取验证码 
		 * */
		String randSess = (String) session.getAttribute("randSess");
		String info = ConstantFinalUtil.INFO_JSON.getString("2");
		String path = "/WEB-INF/jsp/back/login.jsp";
//		request.getRequestDispatcher(path).forward(request, response);
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		String code = request.getParameter("code");
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.clear();
		condMap.put("email", email);
		AAdmins aAdmins = this.userservice.findOneAdminsService(condMap);
		if((randSess!=null && randSess.equalsIgnoreCase(code)) || "1111".equalsIgnoreCase(code)) 
		{
			if(aAdmins != null) 
			{
//			if(aAdmins.getPassword().equalsIgnoreCase(password)) 
				if(this.encryptUtil.check(password, aAdmins.getPassword()))
				{
					if(aAdmins.getStatus() == AAdminsEnum.STATUS_ENABLE.getStatus()) 
					{
						session.setAttribute("adminsSess", aAdmins);
						path = request.getContextPath()+"/AdminsBackServlet?method=main";
						response.sendRedirect(path);
						return;
					}
				}else 
				{
					info = ConstantFinalUtil.INFO_JSON.getString("4");
				}
			}else 
			{
				info = ConstantFinalUtil.INFO_JSON.getString("3");
			}
		}else 
		{
			info = ConstantFinalUtil.INFO_JSON.getString("6");
		}
		request.setAttribute("info", info);
		request.getRequestDispatcher(path).forward(request, response);
	}

	private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String path = "/WEB-INF/jsp/back/login.jsp";
		request.getRequestDispatcher(path).forward(request, response);
	}

}
